package com.da.finance;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.da.landlord.DaRoom;
import com.espirit.eap.manager.TaskSchedulerService;
import com.googlecode.cswish.annotation.Rest;
import com.googlecode.cswish.model.PageInfo;
import com.googlecode.cswish.model.Reference;
import com.googlecode.cswish.oosearch.Condition;
import com.googlecode.cswish.oosearch.QLInfo;
import com.googlecode.cswish.oosearch.QLParse;
import com.googlecode.cswish.struts.hibernate.HibernateUtil;
import com.googlecode.cswish.struts.spring.GenericService;
import com.opensymphony.xwork2.ActionContext;
import com.ra.landlord.Fee;
import com.ra.landlord.LandlordOwnerUser;
import com.ra.landlord.LandlordUserService;
import com.ra.landlord.Room;
import com.ra.landlord.SimpleRoom;
import com.ra.util.DateTimeUtil;

@Service
public class DaFinanceStatementService {

	@Resource
	private GenericService genericService;
	
	@Resource
	private LandlordUserService landlordUserService;
	
	@Resource
	private QLParse qlParse;
	
	@PersistenceContext(unitName="espirit")
	protected EntityManager em;
	
	@Resource
	private TaskSchedulerService taskSchedulerService;
	
	@Rest(type="search")
	public PageInfo<DaFinanceStatement> search(DaFinanceStatement model,Date tradeDate_start,Date tradeDate_end,Reference<QLInfo> qlInfoRef,int pageIndex,int pageSize){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		final Date start = tradeDate_start;
		final Date end = tradeDate_end;
		String roomName = null;
		if (pageSize == 0) {
			pageSize = 15;
			pageIndex = 1;
		}
		if (model.getRoom() != null) {
			roomName = model.getRoom().getName();
		}
		model.setRoom(null);
		model.setStatus(1);
		
		Map<String, Object> searchModel = qlParse.toSearchModel(model, null);
		QLInfo whereQlInfo = qlParse.getWhereClause("a", searchModel, Condition.DEFAULT);
		String where = whereQlInfo.getQl();
		
		QLInfo qlInfo = new QLInfo();
		String ql;
		if (roomName == null || roomName.length() == 0) {
			ql = "from " + DaFinanceStatement.class.getName() + " a";
			if (where.length() > 0) {
				ql += " where " + where;
			}
		} else {
			//因为去除了关联，所以用left join 将无法执行hql 的转换
			/*ql= "from " + DaFinanceStatement.class.getName() + " a left join a.room r left join a.raRoom rm where (r.name like ? or rm.name like ?)";*/
			ql =" from "+DaFinanceStatement.class.getName()+" a ";
			ql += " where 1=1";
			ql +=" and ( (select count(r.id) from "+DaRoom.class.getName()+" r where r.name like ? and r.id = a.daRoomId)>0";
			ql += " or(select count(rm.id) from "+Room.class.getName()+" rm where rm.name like ? and rm.id = a.raRoomId) >0 )";
			qlInfo.addParam("%"+roomName+"%");
			qlInfo.addParam("%"+roomName+"%");
			if (where.length() > 0) {
				ql += " and " + where;
			}
		}
		qlInfo.setQl(ql);
		qlInfo.setDisablePermissionFilter(false);
		for (Object param : whereQlInfo.getParams()) {
			qlInfo.addParam(param);
		}
		if (start != null) {
			qlInfo.andClause("a.tradeDate >=?");
			qlInfo.addParam(start);
		}
		if (end != null) {
			qlInfo.andClause("DATE_FORMAT(a.tradeDate, '%Y-%m-%d') <= ?");
			qlInfo.addParam(sdf.format(end));
		}
		
		qlInfo.setFirstResult((pageIndex - 1) * pageSize);
		qlInfo.setMaxResults(pageSize);
		qlInfo.setCompiledSort("a.id");	
		qlInfo.setDir("desc");
		int totalCount = genericService.getCount(qlInfo, false);
		PageInfo<DaFinanceStatement> pageInfo =  genericService.searchByQl(qlInfo);
		for (DaFinanceStatement financeStatement : pageInfo.getData()) {
			if (financeStatement.getDaRoomId() != null) {
				financeStatement.setRoom(genericService.load(DaRoom.class, financeStatement.getDaRoomId()));
			} else if (financeStatement.getRaRoomId() != null) {
				financeStatement.setRaRoom(genericService.load(Room.class, financeStatement.getRaRoomId()));
			}
		}
		pageInfo.setPageSize(pageSize);
		pageInfo.setTotalCount(totalCount);
		return pageInfo;
	}
	
	@Rest(type="search")
	public List<RoomVO> getAllRoom(){
		//return genericService.searchAll(new DaRoom()).getData();
		QLInfo qlInfo1 = new QLInfo("select a from DaRoom a", false);
		QLInfo qlInfo2 = new QLInfo("select a from " + SimpleRoom.class.getName() + " a", false);
		List<DaRoom> daRooms = genericService.searchByQl(qlInfo1).getData();
		List<SimpleRoom> raRooms = genericService.searchByQl(qlInfo2).getData();
		List<RoomVO> rooms = new ArrayList<>(daRooms.size()+raRooms.size());
		
		for (DaRoom room : daRooms) {
			RoomVO roomVO = new RoomVO();
			roomVO.setId(room.getId());
			roomVO.setName(room.getName());
			roomVO.setRentType(room.getSuite().getRentType());
			roomVO.setRoomType(RoomVO.ROOM_TYPE_DA);
			roomVO.setSuiteName(room.getSuite().getName());
			rooms.add(roomVO);
		}
		
		for (SimpleRoom room : raRooms) {
			RoomVO roomVO = new RoomVO();
			roomVO.setId(room.getId());
			roomVO.setName(room.getName());
			roomVO.setRoomType(RoomVO.ROOM_TYPE_RA);
			roomVO.setSuiteName(room.getBuilding().getName());
			rooms.add(roomVO);
		}
		
		return rooms;
	}
	
	@Rest(type="add")
	public DaFinanceStatement add(DaFinanceStatement model, Integer roomType){
		if (roomType.equals(RoomVO.ROOM_TYPE_RA)) {
			model.setRaRoomId(model.getRoom().getId());
		} else {
			model.setDaRoomId(model.getRoom().getId());
		}
		model.setCreateDate(new Date());
		model.setCreateUser(landlordUserService.getLoginUser());
		model.setStatus(1);
		return genericService.add(model);
	}
	
	@Rest(type="add")
	public DaFinanceStatement addInternal(DaRoom room,BigDecimal tradeMoney,Integer financyCategoryId,Date tradeDate,Integer tradeType,String remark){
		DaFinanceStatement model=new DaFinanceStatement();
		
		financyCategoryId=financyCategoryId==null?1:financyCategoryId;
		tradeType=tradeType==null?0:tradeType;
		tradeDate=tradeDate==null?new Date():tradeDate;
		
		DaFinanceCategory financeCategory=new DaFinanceCategory();
		financeCategory.setId(financyCategoryId);
		//持久状态  维护关系(对于游离状态，关系为reflus建立关联关系则不会维护关系） 改为marge 应该可以。
		genericService.load(financeCategory);
		model.setDaRoomId(room.getId());
		model.setFinanceCategory(financeCategory);
		model.setTradeType(tradeType);
		model.setTradeDate(tradeDate);
		model.setTradeMoney(tradeMoney);
		model.setRemark(remark);
		model.setLandlordUser(room.getLandlordUser());
		model.setCreateUser(room.getLandlordUser());
		model.setCreateDate(new Date());
		//model.setCreateUser(landlordUserService.getLoginUser());
		model.setStatus(1);
		return genericService.add(model);
	}
	
	
	@Rest(type="add")
	public DaFinanceStatement addInternalRa(Room room,BigDecimal tradeMoney,Integer financyCategoryId,Date tradeDate,Integer tradeType,String remark){
		DaFinanceStatement model=new DaFinanceStatement();
		
		financyCategoryId=financyCategoryId==null?1:financyCategoryId;
		tradeType=tradeType==null?0:tradeType;
		tradeDate=tradeDate==null?new Date():tradeDate;
		
		DaFinanceCategory financeCategory=new DaFinanceCategory();
		financeCategory.setId(financyCategoryId);
		//持久状态  维护关系(对于游离状态，关系为reflus建立关联关系则不会维护关系） 改为marge 应该可以。
		genericService.load(financeCategory);
		model.setDaRoomId(room.getId());
		model.setFinanceCategory(financeCategory);
		model.setTradeType(tradeType);
		model.setTradeDate(tradeDate);
		model.setTradeMoney(tradeMoney);
		model.setRemark(remark);
		
		model.setCreateDate(new Date());
		model.setCreateUser(landlordUserService.getLoginUser());
		model.setStatus(1);
		return genericService.add(model);
	}
	
	
	@Rest(type="delete")
	public Boolean delete(List<DaFinanceStatement> model) {
		for (DaFinanceStatement fs : model) {
			DaFinanceStatement dbFinanceStatement = genericService.load(fs);
			dbFinanceStatement.setStatus(0);
			genericService.merge(dbFinanceStatement);
		}
		return true;
	}
	
	/**
	 * 根据费用删除对应的财务
	 * @param fee
	 */
	@Rest(type="delete")
	public void deleteWithFee(Fee fee) {
		DaFinanceStatement example = new DaFinanceStatement();
		example.setFee(fee);
		List<DaFinanceStatement> dbFinanceStatements = genericService.searchByModel(example, Condition.DEFAULT).getData();
		List<Object> list = Arrays.asList(dbFinanceStatements.toArray());
		genericService.deleteList(list);
	}
	
	@Rest(type="update")
	public void _update(DaFinanceStatement model){
		DaFinanceStatement dbFinanceStatement = genericService.load(model);
		if (dbFinanceStatement.getDaRoomId() != null) {
			dbFinanceStatement.setRoom(genericService.load(DaRoom.class, dbFinanceStatement.getDaRoomId()));
		} else if (dbFinanceStatement.getRaRoomId() != null) {
			dbFinanceStatement.setRaRoom(genericService.load(Room.class, dbFinanceStatement.getRaRoomId()));
		}
		ActionContext.getContext().getValueStack().set("model", dbFinanceStatement);
	}
	
	@Rest(type="update")
	public DaFinanceStatement update(DaFinanceStatement model, Integer roomType){
		
		DaFinanceStatement dbfinanceStatment = genericService.load(DaFinanceStatement.class, model.getId());
		if (roomType.equals(RoomVO.ROOM_TYPE_RA)) {
			model.setRaRoomId(model.getRoom().getId());
		} else {
			model.setDaRoomId(model.getRoom().getId());
		}
		model.setCreateDate(dbfinanceStatment.getCreateDate());
		model.setCreateUser(dbfinanceStatment.getCreateUser());
		return genericService.merge(model);
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Rest(type="search")
	public List getAllTransaction(){
		QLInfo ql=new QLInfo("select distinct a.transaction from DaFinanceCategory a where a.type=1", true);
		List list= genericService.searchByQl(ql).getData();
		QLInfo ql2=new QLInfo("select distinct a.transaction from DaFinanceCategory a where a.type=0", false);
		list.addAll(genericService.searchByQl(ql2).getData());
		return list;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Rest(type="search")
	public List getAllTradeCategory(){
		QLInfo ql=new QLInfo("select distinct a.tradeCategory from DaFinanceCategory a where a.type=1", true);
		List list= genericService.searchByQl(ql).getData();
		QLInfo ql2=new QLInfo("select distinct a.tradeCategory from DaFinanceCategory a where a.type=0", false);
		list.addAll(genericService.searchByQl(ql2).getData());
		return genericService.searchByQl(ql).getData();
	}
	
	/**
	 * 新增百万活动财务流水
	 * @param room 房间
	 * @param amount 
	 * @param remark
	 * 
	 */
	public void createActivityStatement(DaFinanceStatement statement){
		DaFinanceCategory dbFinanceCategory_activity = genericService.load(DaFinanceCategory.class, DaFinanceCategory.FINANCE_CATEGORY_ACTIVITY);
		Date currentTime = Calendar.getInstance().getTime();
		statement.setCreateDate(currentTime);
		statement.setFinanceCategory(dbFinanceCategory_activity);
		statement.setTradeDate(currentTime);
		statement.setTradeType(DaFinanceStatement.TRADE_TYPE_BANK_TRANSFER);
		statement.setStatus(1);
		genericService.add(statement);
	}
	
	/**
	 * 新增百万活动财务流水(DA)
	 * @param room 房间
	 * @param amount 
	 * @param remark
	 * 
	 */
	public void createActivityStatementOfDa(DaRoom room, BigDecimal amount, String remark){
		DaFinanceStatement statement = new DaFinanceStatement();
		statement.setDaRoomId(room.getId());
		statement.setCreateUser(room.getLandlordUser());
		statement.setLandlordUser(room.getLandlordUser());
		statement.setRemark(remark);
		statement.setTradeMoney(amount);
		createActivityStatement(statement);
	}
	
	/**
	 * 新增百万活动财务流水(RA)
	 * @param room 房间
	 * @param amount 
	 * @param remark
	 * 
	 */
	public void createActivityStatementOfRa(Room room, BigDecimal amount, String remark){
		DaFinanceStatement statement = new DaFinanceStatement();
		statement.setRaRoomId(room.getId());
		statement.setCreateUser(room.getLandlordUser());
		statement.setLandlordUser(room.getLandlordUser());
		statement.setRemark(remark);
		statement.setTradeMoney(amount);
		createActivityStatement(statement);
	}
	
	public void autoCreateFinanceStatement(Fee fee) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		DaFinanceCategory dbFinanceCategory_rent = genericService.load(DaFinanceCategory.class, DaFinanceCategory.FINANCE_CATEGORY_RENT);
		DaFinanceCategory dbFinanceCategory_deposit = genericService.load(DaFinanceCategory.class, DaFinanceCategory.FINANCE_CATEGORY_DEPOSIT);
		DaFinanceCategory dbFinanceCategory_unRent = genericService.load(DaFinanceCategory.class, DaFinanceCategory.FINANCE_CATEGORY_UNRENT);
		
		if (fee.getAgreement().getRentBeginDate().equals(fee.getDateBeginRecv()) && fee.getAgreement().getRentEndDate().equals(fee.getDateEndRecv())) {
			//押金
			DaFinanceStatement st1 = new DaFinanceStatement();
			st1.setCreateDate(new Date());
			st1.setCreateUser(fee.getAgent());
			st1.setFee(fee);
			st1.setFinanceCategory(dbFinanceCategory_deposit);
			st1.setLandlordUser(fee.getLandlordUser());
			if (HibernateUtil.isValid(fee.getRoom())) {
				//st1.setRaRoom(fee.getRoom());
				st1.setRaRoomId(fee.getRoom().getId());
			}
			
			st1.setRemark(fee.getRemark());
			st1.setTradeDate(DateTimeUtil.StrToDate(fee.getDateBeginRecv()));
			st1.setTradeMoney(fee.getAmountActual());
			if (fee.getPayMode() == null) {
				st1.setTradeType(DaFinanceStatement.TRADE_TYPE_CRASH);
			} else {
				st1.setTradeType(fee.getPayMode());
			}
			st1.setStatus(1);
			genericService.add(st1);
			//租金, 出租只产生的押金
			/*DaFinanceStatement st2 = new DaFinanceStatement();
			st2.setCreateDate(new Date());
			st2.setCreateUser(fee.getAgent());
			st2.setFee(fee);
			st2.setFinanceCategory(dbFinanceCategory_rent);
			st2.setLandlordUser(fee.getLandlordUser());
			if (HibernateUtil.isValid(fee.getRoom())) {
				st2.setRaRoom(fee.getRoom());
			}
			
			st2.setRemark(fee.getRemark());
			st2.setTradeDate(sdf.parse(fee.getDateActual()));
			st2.setTradeMoney(fee.getRental());
			st2.setTradeType(DaFinanceStatement.TRADE_TYPE_CRASH);
			st2.setStatus(1);
			genericService.add(st2);*/
		} //else if (fee.getAgreement().getAgreementState().equals(1) && fee.getAgreement().getFees().get(0) == fee) {
		else if (StringUtils.isNotEmpty(fee.getAgreement().getRentLeaveDate()) && fee.getAgreement().getFees().get(0) == fee) {
			//退租费用
			DaFinanceStatement st1 = new DaFinanceStatement();
			st1.setCreateDate(new Date());
			st1.setCreateUser(fee.getAgent());
			st1.setFee(fee);
			st1.setFinanceCategory(dbFinanceCategory_unRent);
			st1.setLandlordUser(fee.getLandlordUser());
			if (HibernateUtil.isValid(fee.getRoom())) {
				st1.setRaRoomId(fee.getRoom().getId());
			}
			
			st1.setRemark(fee.getRemark());
			st1.setTradeDate(sdf.parse(fee.getDateActual()));
			st1.setTradeMoney(fee.getAmountActual().multiply(BigDecimal.valueOf(-1)));
			if (fee.getPayMode() == null) {
				st1.setTradeType(DaFinanceStatement.TRADE_TYPE_CRASH);
			} else {
				st1.setTradeType(fee.getPayMode());
			}
			st1.setStatus(1);
			genericService.add(st1);
		} else {
			//收租
			DaFinanceStatement st1 = new DaFinanceStatement();
			st1.setCreateDate(new Date());
			st1.setCreateUser(fee.getAgent());
			st1.setFee(fee);
			st1.setFinanceCategory(dbFinanceCategory_rent);
			st1.setLandlordUser(fee.getLandlordUser());
			if (HibernateUtil.isValid(fee.getRoom())) {
				st1.setRaRoomId(fee.getRoom().getId());
			}
			
			st1.setRemark(fee.getRemark());
			st1.setTradeDate(sdf.parse(fee.getDateActual()));
			st1.setTradeMoney(fee.getAmountActual());
			if (fee.getPayMode() == null) {
				st1.setTradeType(DaFinanceStatement.TRADE_TYPE_CRASH);
			} else {
				st1.setTradeType(fee.getPayMode());
			}
			st1.setStatus(1);
			genericService.add(st1);
		}
		
	}
	@Transactional
	public void autoCreateFinanceStatementForVC() throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		DaFinanceCategory dbFinanceCategory_rent = genericService.load(DaFinanceCategory.class, DaFinanceCategory.FINANCE_CATEGORY_RENT);
		DaFinanceCategory dbFinanceCategory_deposit = genericService.load(DaFinanceCategory.class, DaFinanceCategory.FINANCE_CATEGORY_DEPOSIT);
		DaFinanceCategory dbFinanceCategory_unRent = genericService.load(DaFinanceCategory.class, DaFinanceCategory.FINANCE_CATEGORY_UNRENT);
		
		//QLInfo qlInfo = new QLInfo("select a from Fee a where not exists (select b from DaFinanceStatement b where b.fee.id = a.id)", true); //跳过权限
		//QLInfo qlInfo = new QLInfo("select a from Fee a where a.agreement is not null and (a.agreement.status = 1 or a.agreement.status = 2)  and a.id not in (select b.fee.id from DaFinanceStatement b where b.fee.id is not null)", true); //跳过权限
		QLInfo qlInfo = new QLInfo("select a from Fee a where a.simpleAgreement is not null and (a.status = 1 or a.status = 2)  and a.id not in (select b.fee.id from DaFinanceStatement b where b.fee.id is not null)", true); //跳过权限
		List<Fee> fees = genericService.searchByQl(qlInfo).getData();
		List<DaFinanceStatement> financeStatements = new ArrayList<DaFinanceStatement>(fees.size());
		int i = 0;
		for (Fee fee : fees) {
			if (fee.getSimpleAgreement().getRentBeginDate().equals(fee.getDateBeginRecv()) && fee.getSimpleAgreement().getRentEndDate().equals(fee.getDateEndRecv())) {
				//押金
				DaFinanceStatement st1 = new DaFinanceStatement();
				st1.setCreateDate(new Date());
				st1.setCreateUser(fee.getAgent());
				st1.setFee(fee);
				st1.setFinanceCategory(dbFinanceCategory_deposit);
				st1.setLandlordUser(fee.getLandlordUser());
				try {
					st1.setRaRoomId(fee.getRoom().getId());
				} catch (EntityNotFoundException e) {e.printStackTrace();}
				
				st1.setRemark(fee.getRemark());
				st1.setTradeDate(DateTimeUtil.StrToDate(fee.getDateBeginRecv()));
				st1.setTradeMoney(fee.getAmountActual());
				if (fee.getPayMode() == null) {
					st1.setTradeType(DaFinanceStatement.TRADE_TYPE_CRASH);
				} else {
					st1.setTradeType(fee.getPayMode());
				}
				st1.setStatus(1);
				financeStatements.add(st1);
				//租金， 出租只产生了押金
				/*DaFinanceStatement st2 = new DaFinanceStatement();
				st2.setCreateDate(new Date());
				st2.setCreateUser(fee.getAgent());
				st2.setFee(fee);
				st2.setFinanceCategory(dbFinanceCategory_rent);
				st2.setLandlordUser(fee.getLandlordUser());
				if (HibernateUtil.isValid(fee.getRoom())) {
					st2.setRaRoom(fee.getRoom());
				}
				
				st2.setRemark(fee.getRemark());
				st2.setTradeDate(sdf.parse(fee.getDateActual()));
				st2.setTradeMoney(fee.getRental());
				st2.setTradeType(DaFinanceStatement.TRADE_TYPE_CRASH);
				st2.setStatus(1);
				financeStatements.add(st2);*/
			} //else if (fee.getAgreement().getAgreementState().equals(1) && fee.getAgreement().getFees().get(0) == fee) {
			else if (StringUtils.isNotEmpty(fee.getSimpleAgreement().getRentLeaveDate()) && fee.getSimpleAgreement().getFees().get(0) == fee) {
				//退租费用
				DaFinanceStatement st1 = new DaFinanceStatement();
				st1.setCreateDate(new Date());
				st1.setCreateUser(fee.getAgent());
				st1.setFee(fee);
				st1.setFinanceCategory(dbFinanceCategory_unRent);
				st1.setLandlordUser(fee.getLandlordUser());
				try {
					st1.setRaRoomId(fee.getRoom().getId());
				} catch (EntityNotFoundException e) {e.printStackTrace();}
				
				st1.setRemark(fee.getRemark());
				st1.setTradeDate(sdf.parse(fee.getDateActual()));
				st1.setTradeMoney(fee.getAmountActual().multiply(BigDecimal.valueOf(-1)));
				if (fee.getPayMode() == null) {
					st1.setTradeType(DaFinanceStatement.TRADE_TYPE_CRASH);
				} else {
					st1.setTradeType(fee.getPayMode());
				}
				st1.setStatus(1);
				financeStatements.add(st1);
			} else {
				//收租
				DaFinanceStatement st1 = new DaFinanceStatement();
				st1.setCreateDate(new Date());
				st1.setCreateUser(fee.getAgent());
				st1.setFee(fee);
				st1.setFinanceCategory(dbFinanceCategory_rent);
				st1.setLandlordUser(fee.getLandlordUser());
				try {
					st1.setRaRoomId(fee.getRoom().getId());
				} catch (EntityNotFoundException e) {e.printStackTrace();}
				
				st1.setRemark(fee.getRemark());
				st1.setTradeDate(sdf.parse(fee.getDateActual()));
				st1.setTradeMoney(fee.getAmountActual());
				if (fee.getPayMode() == null) {
					st1.setTradeType(DaFinanceStatement.TRADE_TYPE_CRASH);
				} else {
					st1.setTradeType(fee.getPayMode());
				}
				st1.setStatus(1);
				financeStatements.add(st1);
			}
		}
		batchInsert(financeStatements);
		//genericService.addList(financeStatements);
		
		taskSchedulerService.updateTaskSchedulerRunTime(this.getClass(), "autoCreateFinanceStatementForVC");
	}
	
	@Transactional
	protected void batchInsert (List<DaFinanceStatement> financeStatements) {
		for (int i =0; i<financeStatements.size(); i++) {
			em.persist(financeStatements.get(i));
			if (i%30 == 0) {
				em.flush();
				em.clear();
			}
		}
	}
}
